package org.kernely.directory.userpage;

import org.kernely.directory.DirectoryBundle;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Reindeer;

/**
 * View containing users directory.
 */
public class KernelyDirectoryView extends VerticalLayout implements IKernelyView, KernelyEventSender {

	/**
	 * The Serial Version
	 */
	private static final long serialVersionUID = -5465377098197707204L;

	private Layout usersLayout;
	private TextField searchField;

	/**
	 * KernelyProfileView's constructor
	 */
	public KernelyDirectoryView(){
		usersLayout = new VerticalLayout();
		usersLayout.setMargin(true);
		
		Label title = new Label("Directory");
		title.setStyleName(Reindeer.LABEL_H1);
		title.setIcon(new ThemeResource("img/directory32.png"));
		title.setSizeUndefined(); // Usefull to center the label.
		
		searchField = new TextField();
		searchField.setTextChangeEventMode(TextChangeEventMode.LAZY);
		searchField.setTextChangeTimeout(500);
		searchField.setImmediate(true);
		searchField.setInputPrompt("Search...");
		
		Button clearSearchButton = new Button("Clear");
		clearSearchButton.addListener(new ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				searchField.setValue("");
			}
		});
		
		
		VerticalLayout vlayout = new VerticalLayout();
		Panel headerPanel = new Panel();		
		headerPanel.setContent(vlayout);
		vlayout.addComponent(title);
		vlayout.setMargin(true, false, true, false);
		vlayout.setComponentAlignment(title, Alignment.MIDDLE_CENTER);
		headerPanel.setSizeFull();
		
		Panel displayPanel = new Panel();
		VerticalLayout displayLayout = new VerticalLayout();
		displayPanel.setContent(displayLayout);
		
		HorizontalLayout searchLayout = new HorizontalLayout();
		searchLayout.setSizeUndefined();
		searchLayout.addComponent(searchField);
		searchLayout.addComponent(clearSearchButton);
		searchLayout.setComponentAlignment(searchField, Alignment.MIDDLE_LEFT);
		searchLayout.setComponentAlignment(clearSearchButton, Alignment.MIDDLE_LEFT);

		displayPanel.addComponent(searchLayout);
		displayPanel.addComponent(usersLayout);
		
		this.addComponent(headerPanel);
		this.addComponent(displayPanel);
	}
	
	

	
	/**
	 * Gets the usersLayout
	 * @return the usersLayout
	 */
	public Layout getUsersLayout() {
		return usersLayout;
	}




	/**
	 * This method is called when this component is added to an other.
	 */
	@Override
	public final void attach(){
		super.attach();
		//When the component is attached, we fire listeners
		DirectoryBundle.getEventBus().broadcastEvent(new KernelyDirectoryAttachedEvent(this));
	}

	/**
	 * Useless method.
	 */
	public final Component getComponent() {
		return this;
	}




	/**
	 * Gets the searchField
	 * @return the searchField
	 */
	public TextField getSearchField() {
		return searchField;
	}

	
	
}
